from flask_classy import FlaskView, route
from ..models.service import Service
from ..schema.object import success_schema, query_params, service_schema, currency_schema
from flask import request
from . import jsonify_with_success, jsonify_with_error, jsonify_with_data
from ..schema.resp import ServiceWithCurrencyResp
from ..models.currency import Currency


class ServicesView(FlaskView):
    @route('/<id>')
    def fetch_by_id(self, id):
        try:
            service = Service.get_or_none(Service.id == id)

            if not service:
                return success_schema.jsonify()

            params, errors = query_params.load(request.args)
            currency = params.get('currency')
            if not currency:
                currency = Currency.get_main_currency()

            if errors:
                pass
            # if service:
            #     if service.is_fixed_mode():
            #         service.fee = service.fee * currency.rate
            service.currency = currency
            service_with_currency_resp = ServiceWithCurrencyResp()
            service_with_currency_resp.context = {'currency': currency}
            return service_with_currency_resp.jsonify(service)
            # return success_schema.jsonify(
            #     {'currency': currency_schema.dump(currency).data,
            #      'service': service_schema.dump(service).data
            #      }
            # )
        except Currency.DoesNotExist:
            return CURRENCY_NOT_EXIST.jsonify()
            # if service:
